package LinkedList;

class ListNode2 {
    int val;
    ListNode2 next;
    ListNode2() {
    }
    ListNode2(int val) {
        this.val = val;
    }
    ListNode2(int val, ListNode2 next) {
        this.val = val;
        this.next = next;
    }
}


//利用快慢指针，只遍历链表一遍来找到中间节点
class Solution3 {
    public ListNode2 middleNode(ListNode2 head) {
        ListNode2 cur = head;
        ListNode2 db = head;
        if(head == null || head.next == null) {
            return head;
        }
        while (db!=null&&db.next!=null) {
            cur = cur.next;
            db = db.next.next;
        }
        return cur;
    }
}
